package com.bdqn.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.springbootmybatisplus.pojo.SysRole;
import com.bdqn.springbootmybatisplus.pojo.SysUser;
import com.bdqn.springbootmybatisplus.service.SysRoleService;
import com.bdqn.springbootmybatisplus.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SysUserController
 * 用户模块控制器
 * @author
 * @since
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private SysUserService userService;

    @Resource
    private SysRoleService roleService;

    /**
     * 查询用户列表分页显示
     */
    @RequestMapping("/list")
    public String list(Model model, String usrName, Long roleId, Long pageIndex) {
        // 如果为空,查第一页
        if (pageIndex == null) {
            pageIndex = 1L;
        }

        // 查询数据回显
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);

        // 获取所有角色数据填充下拉框
        List<SysRole> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);

        // 使用页面传递的参数分页查询用户列表
        Page<SysUser> page = new Page<>();
        page.setCurrent(pageIndex);
        IPage<SysUser> userPager = userService.findSysUsers(usrName, roleId, page);
        model.addAttribute("userPager", userPager);

        return "user/list";
    }

    /**
     * 去到新增用户页面
     */
    @GetMapping("/add")
    public String toAdd(Model model) {
        // 获取所有角色数据填充下拉框
        List<SysRole> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    /**
     * 去到修改用户页面
     */
    @GetMapping("/edit")
    public String toEdit(Model model, Long usrId) {
        // 获取所有角色数据填充下拉框
        List<SysRole> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);

        // 查询用户信息显示到页面
        SysUser user = userService.getSysUser(usrId);
        model.addAttribute("user", user);
        return "user/edit";
    }

    /**
     * 执行新增或修改用户信息
     */
    @PostMapping("/save")
    public String save(SysUser user) {
        userService.saveSysUser(user);
        return "redirect:/user/list";
    }

    /**
     * 根据ID删除用户信息
     */
    @ResponseBody
    @GetMapping("/del/{usrId}")
    public Object del(@PathVariable("usrId") Long usrId) {
        boolean flag = userService.deleteSysUser(usrId);
        Map map = new HashMap();
        map.put("delResult","true");
        return map;
    }

    // @Resource
    // private SysUserService sysUserService;
    //
    //
    // @Resource
    // private SysRoleService sysRoleService;
    //
    // @RequestMapping("/list")
    // public String list(Model model ,String usrName,Long roleId,Long pageIndex){
    //     // 查询数据回显
    //     model.addAttribute("usrName",usrName);
    //     model.addAttribute("roleId",roleId);
    //     // 获取所有权限数据来填充下拉框
    //     List<SysRole> allRoles = sysRoleService.findAllRoles();
    //     model.addAttribute("roles",allRoles);
    //
    //     // 使用页面传递的参数分页查询用户列表
    //     Page<SysUser> page = new Page<>();
    //     page.setCurrent(pageIndex);
    //     IPage<SysUser> userPager = sysUserService.findSysUsers(usrName, roleId, page);
    //     model.addAttribute("userPager",userPager);
    //
    //     return "user/list";
    // }

    // @GetMapping("/test")
    // public Object test() {
    //     System.out.println("---------------------- /user/update ----------------------");
    //     SysUser sysUser = sysUserService.getById(4L);
    //     sysUser.setUsrName("小苍。。。");
    //     sysUserService.updateById(sysUser);
    //     return sysUser;
    // }
    //
    // @GetMapping("/list")
    // public Object getList() {
    //     System.out.println("---------------------- /user/list ----------------------");
    //     SysUser query = new SysUser();
    //     query.setUsrRoleId(4L);
    //     return sysUserService.getList(query);
    // }
}
